package com.boot.redis.controller;

import com.boot.commons.pojo.UserDel;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

@RestController
public class UserController {


    /**
     * 开启缓存 ,如缓存的key一致,则不再执行方法内容
     *
     * @return
     */

    @RequestMapping("/getUser")
    @Cacheable(value = "user-key")
    public UserDel getUser() {
        UserDel userDel = new UserDel("aa@126.com", "aa", "aa123456", "aa", "123");
        System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
        UserDel userDel1 = userDel;


        return userDel1;
    }


    @RequestMapping("/uid")
    String uid(HttpSession session, HttpServletResponse rp) {


        UUID uid = (UUID) session.getAttribute("uid");
        if (uid == null) {
            uid = UUID.randomUUID();
        }
        session.setAttribute("uid", uid);
        return session.getId();
    }
}